package leetcode.L500;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

public class Ex215 {

    public static void main(String[] args) {
        Queue<Integer> queue = new PriorityQueue<>(5, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });

        queue.add(4);
        queue.add(8);
        queue.add(1);

        while(!queue.isEmpty()){
            System.out.println(queue.poll());
        }

    }

    public int findKthLargest(int[] nums, int k) {
        Queue<Integer> queue = new PriorityQueue<>(k, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;
            }
        });
        for (int i = 0; i < nums.length; i++) {
            if(queue.size()<k||queue.peek()<nums[i]){
                queue.add(nums[i]);
            }
        }
        return queue.peek();
    }
}
